/* 
 * File:   Sphere.cpp
 * Author: gilvolpe
 * 
 * Created on May 28, 2013, 4:18 PM
 */

#include "Planet.h"

Planet::Planet(GALATICS_ELEMENTS elem,
        Point3D point, GLfloat update = 0) : Objects(point, update) {

    GLfloat factorRadius = 1.0f;
    GLfloat factorRadiusPlanets = SUN_RADIUS / 1.5f;
    GLfloat factorPosition = 1.0f;
   
    Point3D pointPlanet;
    pointPlanet._z = 0.0f;

    versor._x = 0.0f;
    versor._y = 0.0f;
    versor._z = 1.0f;

    Material mat;
    GLfloat position;

    switch (elem) {
        case SUN:
            //pointPlanet._x = 0.0f * factorPosition;

            planet.id_texture = getTex().loadBitmap("texturas/sol.bmp");
            this->radiusOfSphere = SUN_RADIUS;
            angleOfRotationUpdate = SUN_ROTATION;
            position = 0.0f;
            mat = getMaterial();
            mat.setSunLight();
            mat.Apply();

            setMaterial(mat);

            break;

        case MER:

            planet.id_texture = getTex().loadBitmap("texturas/mercurio.bmp");
            this->radiusOfSphere = MER_RADIUS * factorRadiusPlanets;
            angleOfRotationUpdate = MER_ROTATION;

            position = FACTOR_MER;

            break;
        case VEN:
            pointPlanet._x = 10.8 * factorPosition;
            planet.id_texture = getTex().loadBitmap("texturas/venus.bmp");
            this->radiusOfSphere = VEN_RADIUS * factorRadiusPlanets;
            angleOfRotationUpdate = VEN_ROTATION;

            position = FACTOR_VEN;
            break;
        case EAR:

            planet.id_texture = getTex().loadBitmap("texturas/terra.bmp");
            this->radiusOfSphere = EAR_RADIUS * factorRadius*factorRadiusPlanets;
            angleOfRotationUpdate = EAR_ROTATION;

            position = FACTOR_EAR;
            break;
        case MAR:

            planet.id_texture = getTex().loadBitmap("texturas/marte.bmp");
            this->radiusOfSphere = MAR_RADIUS * factorRadius*factorRadiusPlanets;
            angleOfRotationUpdate = MAR_ROTATION;

            position = FACTOR_MAR;
            break;
        case JUP:

            planet.id_texture = getTex().loadBitmap("texturas/jupter.bmp");
            this->radiusOfSphere = JUP_RADIUS * factorRadius*factorRadiusPlanets;
            angleOfRotationUpdate = JUP_ROTATION;

            position = FACTOR_JUP;

            break;
        case SAT:

            planet.id_texture = getTex().loadBitmap("texturas/saturno.bmp");
            this->radiusOfSphere = SAT_RADIUS * factorRadius*factorRadiusPlanets;
            angleOfRotationUpdate = SAT_ROTATION;

            position = FACTOR_SAT;

            break;
        case URA:

            planet.id_texture = getTex().loadBitmap("texturas/urano.bmp");
            this->radiusOfSphere = URA_RADIUS * factorRadius*factorRadiusPlanets;
            angleOfRotationUpdate = URA_ROTATION;

            position = FACTOR_URA;

            break;
        case NET:

            planet.id_texture = getTex().loadBitmap("texturas/netuno.bmp");
            this->radiusOfSphere = NET_RADIUS * factorRadius*factorRadiusPlanets;
            angleOfRotationUpdate = NET_ROTATION;

            position = FACTOR_NET;
            break;
        case MOO:
            //            planet.id_texture = LoadBitmap("texturas/lua.bmp");
            this->radiusOfSphere = MOO_RADIUS * factorRadius*factorRadiusPlanets;
            break;
        default:
            cout << "Problem, neither valid element" << endl;
            exit(0);
            break;
    }

    angleOfRotation = 0.0f;
    angleOfRotationUpdate /= 1000.0f;

    pointPlanet._x = position;
    setPoint(pointPlanet);

    radiusOfRotation = getPoint()._x;



    this->qobj = gluNewQuadric();
    gluQuadricDrawStyle(this->qobj, GLU_FILL);
    gluQuadricNormals(this->qobj, GLU_SMOOTH);
    gluQuadricTexture(qobj, GL_TRUE);

    glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
}

Planet::~Planet() {
}

void Planet::specificDraw() {

    calcPosition();
    Point3D point = getPoint();
    glTranslatef(point._x, point._y, point._z);

    glRotatef(270.0f, 1.0f, 0.0f, 0.0f); //colocar o planeta no sentido correto
    glRotatef(-getAngle(), 0.0f, 0.0f, 1.0f);

    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D, planet.id_texture);

    gluSphere(this->qobj, this->radiusOfSphere, SLICES, STACKS);

    glDisable(GL_TEXTURE_2D);
}

void Planet::setRadius(GLfloat radius) {
    this->radiusOfRotation = radius;
}

GLfloat Planet::getRadius() {
    return this->radiusOfRotation;
}

void Planet::specificUpdate() {

    GLfloat angle = getAngle();
    GLfloat update = getUpdateAngle();

    angle += update;

    if (angle >= 360)
        angle = 0;

    setAngle(angle);
}

void Planet::calcPosition() {

    angleOfRotation += angleOfRotationUpdate;

    if (angleOfRotation >= 360.0f)
        angleOfRotation = 0.0f;

    Point3D point = getPoint();
    point._z = radiusOfRotation * sin(angleOfRotation);
    point._x = radiusOfRotation * cos(angleOfRotation);

    setPoint(point);
}